DEBUG=1
DIR_OUTPUT_H=/usr/local/include
DIR_OUTPUT_SO=/usr/local/lib

###############################################################################
# CAUTION! Modify below only if you are famaliar with Make

current_dir := $(shell pwd)
SHELL := /bin/bash

.PHONY : all, clean, install, uninstall, check

all: libcmpcxx.so \
test_path_funcs.out

ifeq ($(DEBUG), 1) 
CFLAGS=-O0 -g
config_label='Debug'
else
CFLAGS=-Ofast
config_label='Release'
endif
CFLAGS+=-std=c++11

output_h=cmpcxx.h
output_tpl_h=cmpcxx.tpl.h
output_so=libcmpcxx.so
objects=io.o log.o math.o path.o
common_prerequisites=Makefile $(output_h)
ldflags_this=-L$(current_dir)
ldlibs_this=-lcmpcxx

$(output_so):$(objects) $(common_prerequisites)
	gcc $(CFLAGS) -shared $(objects) -o $(output_so)

test_path_funcs.out: test_path_funcs.cpp $(common_prerequisites) $(output_so)
	g++ $(CFLAGS) test_path_funcs.cpp -o test_path_funcs.out $(ldflags_this) $(ldlibs_this)

io.o: io.cpp $(common_prerequisites)
	g++ $(CFLAGS) -fpic -c io.cpp -o io.o

log.o: log.cpp $(common_prerequisites)
	g++ $(CFLAGS) -fpic -c log.cpp -o log.o
	
math.o: math.cpp $(common_prerequisites)
	g++ $(CFLAGS) -fpic -c math.cpp -o math.o

path.o: path.cpp $(common_prerequisites)
	g++ $(CFLAGS) -Wno-format-truncation -fpic -c path.cpp -o path.o

clean : 
	-rm -v *.o
	-rm -v *.out
	-rm -v *.so

check : 
	@echo "Is for Debug or Release:" $(config_label)
	@echo "Path to install the header:" $(DIR_OUTPUT_H)/$(output_h)
	@echo "Path to install the so:" $(DIR_OUTPUT_SO)/$(output_so)

install : $(output_so) $(output_h) $(output_tpl_h)
	cp -iv $(output_h) $(DIR_OUTPUT_H)
	chmod -v 0444 $(DIR_OUTPUT_H)/$(output_h)
	cp -iv $(output_tpl_h) $(DIR_OUTPUT_H)
	chmod -v 0444 $(DIR_OUTPUT_H)/$(output_tpl_h)
	cp -iv $(output_so) $(DIR_OUTPUT_SO)
	chown -v root:root $(DIR_OUTPUT_SO)/$(output_so)
	chmod -v 0755 $(DIR_OUTPUT_SO)/$(output_so)

uninstall :
	rm -v $(DIR_OUTPUT_H)/$(output_h)
	rm -v $(DIR_OUTPUT_SO)/$(output_so)